import baseUrl from '../../api/baseUrl'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl:'',
    imgPath:''
  },
  getUserInfo(){
    var _this = this
    wx.getUserInfo({
      success(res){
        console.log(res);
        _this.setData({
          avatarUrl:res.userInfo.avatarUrl
        })
      },
      fail(error){
        console.log(error);
      }
    })
  },
  getUserProfile(){
   wx.getUserProfile({
     desc: '获取用户信息',
     success(res){
       console.log(res);
     },
     fail(error){
       console.log(error);
     }
   })
  },
  btnGetUserInfo(e){
    console.log(e);
  },
  getImgTempUrl(e){
    console.log(e);
    this.setData({
      avatarUrl:e.detail.avatarUrl
    })
  },
  sacncode(){
    wx.scanCode({
      scanType:['qrCode','pdf417','datamatrix','barCode'],
      onlyFromCamera:false,
      success(res){
        console.log(res);
      },
      fail(error){
        console.log(error);
      }
    })
  },
  uploadImg(){
    var _this = this
    // 1. 选择图片
    wx.chooseImage({
      count:1,
      sizeType:['original'],
      sourceType:['album','camera'],
      success(res){
        console.log(res);
        let filePath = res.tempFilePaths[0]
        // 2. 把图片上传到服务器
        wx.uploadFile({
          // 服务器地址
          url: baseUrl+'/api/test/user/upload',
          // 临时文件路径
          filePath: filePath,
          name: 'file',
          timeout:5000,
          success(res2){
            console.log(res2);
            let imgPath = baseUrl+ "/" +JSON.parse(res2.data).data
            _this.setData({
              imgPath
            })
          }
        })
      }
    })
  },
  uploadImg2(){
    var _this = this
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['camera','album'],
      sizeType:['original'],
      success(res){
        console.log(res);
        let filePath = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          // 服务器地址
          url: baseUrl+'/api/test/user/upload',
          // 临时文件路径
          filePath: filePath,
          name: 'file',
          timeout:5000,
          success(res2){
            console.log(res2);
            let imgPath = baseUrl+ "/" +JSON.parse(res2.data).data
            _this.setData({
              imgPath
            })
          }
        })
      }
    })
  }
})